package cn.rengy.web.framework.wxmp.subscribemsg;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import cn.rengy.tool.core.util.ExceptionPrintUtils;
import cn.rengy.tpa.wxmp.message.subscribemsg.SubscribeMsg;
import cn.rengy.tpa.wxmp.message.subscribemsg.SubscribeMsgRequest;
import cn.rengy.tpa.wxmp.token.AccessToken;
import cn.rengy.web.framework.property.service.PropertyService;
import cn.rengy.web.framework.sql.DbService;
import cn.rengy.web.framework.wxmp.token.AccessTokenCacheService;

@Service
public class SubscribeMsgService {
	private static Logger logger = LoggerFactory.getLogger(SubscribeMsgService.class);
	@Autowired
	private DbService dbService;
	@Autowired
	private AccessTokenCacheService accessTokenCacheService;
	@Autowired
	private PropertyService propertyService;
	public ModelAndView subscribemsg(String openid,String template_id,String reserved,String action,int scene){
		ModelAndView view=new ModelAndView();
		view.setViewName("/ftl/mobile/subscribemsg/index");
		if("confirm".equals(action)){
			String appid = propertyService.getProperty("DEFAULT_WX_APPID");
			AccessToken accessToken=accessTokenCacheService.getAccessToken(appid);
			SubscribeMsg subscribeMsg=new SubscribeMsg(accessToken);
			//domain
			String url="http://www.lvzhi.com/wx/subscribemsg/index";
			String title="订阅消息标题";
			String value="订阅消息内容\n换行\n换行";
			String color="#173177";
			SubscribeMsgRequest subscribeMsgRequest=new SubscribeMsgRequest(openid, template_id, url,  scene, title,
					 value,  color);
			try {
				subscribeMsg.send(subscribeMsgRequest);
			} catch (Exception e) {
				logger.error(ExceptionPrintUtils.getTrace(e));
			}
		}else{
			
		}
		return view;
	}
	/**
	 * 显示模板消息内容
	 * @return
	 */
	public ModelAndView index(){
		ModelAndView view=new ModelAndView();
		view.setViewName("/ftl/mobile/subscribemsg/index");
		return view;
	}
	
}
